use diesel::prelude::*;

use crate::{database::schema, route};

#[derive(Queryable, Selectable)]
#[diesel(table_name = schema::link)]
#[diesel(check_for_backend(diesel::mysql::Mysql))]
pub struct Link {
    pub id: i32,
    pub platform: String,
    pub url: String,
    pub tid: i32,
}

#[derive(Insertable, AsChangeset)]
#[diesel(table_name = schema::link)]
pub struct NewLink<'a> {
    pub platform: &'a str,
    pub url: &'a str,
    pub tid: i32,
}

impl<'a> From<&'a route::form::link::NewLinkForm> for NewLink<'a> {
    fn from(link: &'a route::form::link::NewLinkForm) -> Self {
        NewLink {
            platform: &link.platform,
            url: &link.url,
            tid: link.tid,
        }
    }
}

impl<'a> From<&'a Link> for NewLink<'a> {
    fn from(link: &'a Link) -> Self {
        NewLink {
            platform: &link.platform,
            url: &link.url,
            tid: link.tid,
        }
    }
}
